﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;
using QuanLyCayXanh.Models;

namespace QuanLyCayXanh.Controllers
{
    public class AddressController : Controller
    {
        private QuanLyCayXanhContext db = new QuanLyCayXanhContext();
        //
        // GET: /Address/

        public ActionResult Index()
        {
            return View(db.Addresses.Include("Ward.District").ToList());
        }
        public ActionResult Create()
        {
            ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name");            
            ViewBag.Ward_Id = null;
            ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name");
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Address address)
        {
            if (ModelState.IsValid)
            {
                db.Addresses.Add(address);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(address);
        }

        public ActionResult Edit(int id = 0)
        {
            Address address = db.Addresses.Find(id);
            if (address == null)
                return HttpNotFound();
            
            if (address.Street != null)
                ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name", address.Street.Id);
            else
                ViewBag.Street_Id = new SelectList(db.Streets, "Id", "Name");

            if (address.Ward != null)
            {
                ViewBag.Ward_Id = new SelectList(db.Wards, "Id", "Name", address.Ward.Id);
                ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name", address.Ward.District.Id);
            }
            else
            {
                ViewBag.Ward_Id = null;
                ViewBag.District_Id = new SelectList(db.Districts, "Id", "Name");
            }           
            
            return View(address);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]

        public ActionResult Edit(Address address)
        {
            if (ModelState.IsValid)
            {
                db.Entry(address).State = System.Data.EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(address);
        }
        public ActionResult Details(int id = 0)
        {
            Address address = db.Addresses.Find(id);
            if (address == null)
                return HttpNotFound();
            return PartialView(address);
        }

        public ActionResult Delete(int id = 0)
        {
            Address address = db.Addresses.Find(id);
            if (address == null)
                return HttpNotFound();
            return View(address);
        }
        [HttpPost, ActionName("Delete")]        
        public ActionResult DeleteConfirmed(int id = 0)
        {
            Address address = db.Addresses.Find(id);
            db.Addresses.Remove(address);
            db.SaveChanges();
            return Content(Boolean.TrueString);
        }

        
        public ActionResult SelectDistrict(int? District_Id)
        {
            District district = new District();
            district.Wards = new List<Ward>();
            if (District_Id.HasValue)
            {
                district.Wards = (from s in db.Wards.Include(m => m.District)
                         where s.District_Id == District_Id
                         orderby s.Name
                         select s).ToList();
            }
            return View(district);            
        }

        [HttpPost]
        public ActionResult _GetSelectWard(string District_Id)
        {
            int id = int.Parse(District_Id);
            var wards = db.Wards.Where(s => s.District_Id == id).ToList();
            ViewBag.Ward_Id = new SelectList(wards, "Id", "Name");
            return PartialView("_SelectWard");
        }
    }
}
